package com.findviewbydream.evil.helloyouth;

import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;

import com.bumptech.glide.Glide;
import com.findviewbydream.evil.helloyouth.Data.UserData;
import com.findviewbydream.evil.helloyouth.SlideMenu.LeftFragment;
import com.findviewbydream.evil.helloyouth.SlideMenu.RightFragment;
import com.findviewbydream.evil.helloyouth.Utils.SHandler;
import com.findviewbydream.evil.helloyouth.Utils.SuperToast.ProgressToast;
import com.findviewbydream.evil.helloyouth.Utils.SuperToast.SuperToast;
import com.findviewbydream.evil.helloyouth.Utils.Utils;
import com.findviewbydream.evil.helloyouth.View.CircleTextView;
import com.mmga.metroloading.MetroLoadingView;

import de.hdodenhof.circleimageview.CircleImageView;

/********************************************
 * 工程名：HelloYouth
 * 包名：com.findviewbydream.evil.helloyouth
 * 时间：16-6-1 下午7:48
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：主界面
 ********************************************/
public class MainActivity extends AppCompatActivity
{
    private static MainActivity mainActivity;

    /*顶部栏*/
    private Toolbar toolbar;
    protected static MetroLoadingView loadingView;
    private CircleTextView indiactor;//角标提示
    private CircleImageView userImg;/*顶部圆形头像*/

    /*主界面 中，左，右*/
    private FrameLayout container_main;
    protected static FrameLayout container_left;
    protected static FrameLayout container_right;

    /*顶部事项*/
    protected static DrawerLayout drawerlayout;
    private ActionBarDrawerToggle barDrawerToggle;

    /*Fragment管理*/
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private Fragment fragment;/*当前的Fragment 默认Fragment*/


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        UserData.initData(this);/*初始化全局数据*/

        initView();/*初始化控件*/
        initFragment();/*初始化Fragment*/
        initSlidebar();/*初始化侧滑栏*/

        setToolbar();/*设置顶部栏*/
        setDrawerlayout();/*设置侧滑栏*/
    }


    /**
     * 初始化控件
     */
    private void initView()
    {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        loadingView = (MetroLoadingView) findViewById(R.id.loadingView);
        container_main = (FrameLayout) findViewById(R.id.container_main);
        container_left = (FrameLayout) findViewById(R.id.container_left);
        container_right = (FrameLayout) findViewById(R.id.container_right);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);

        userImg = (CircleImageView) findViewById(R.id.userImg);
        indiactor = (CircleTextView) findViewById(R.id.indiactor);
    }

    /**
     * 初始化Fragment
     */
    private void initFragment()
    {
        /*Fragment管理初始化*/
        {
            fragmentManager = getSupportFragmentManager();
            transaction = fragmentManager.beginTransaction();
        }

        /*默认Fragment*//*
        if (fragment == null)
        {
            fragment = HomeFragment.newInstance();
            transaction.add(R.id.container_main, fragment);
        }*/
    }

    /**
     * 初始化侧滑栏
     */
    private void initSlidebar()
    {
        if (fragmentManager.findFragmentById(R.id.container_right) == null)
            transaction.add(R.id.container_right, RightFragment.newInstance(null));
        if (fragmentManager.findFragmentById(R.id.container_left) == null)
            transaction.add(R.id.container_left, LeftFragment.newInstance(null)).commit();
    }

    /**
     * 设置顶部栏
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void setToolbar()
    {
        /*设置用户头像*/
        Glide.with(this).load(R.drawable.userimage)
                .placeholder(R.drawable.userimage).into(userImg);
        toolbar.setTitle("");

        setSupportActionBar(toolbar);

        indiactor.setBackgroundColor(Color.RED);


        /*toolbar.setNavigationOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (drawerlayout.isDrawerOpen(container_left))
                    drawerlayout.closeDrawer(container_left);
                else
                    drawerlayout.openDrawer(container_left);
            }
        });*/

        userImg.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (drawerlayout.isDrawerOpen(container_left))
                    drawerlayout.closeDrawer(container_left);
                if (drawerlayout.isDrawerOpen(container_right))
                    drawerlayout.closeDrawer(container_right);
                else
                    drawerlayout.openDrawer(container_right);
            }
        });
    }

    /**
     * 设置侧滑栏
     */
    private void setDrawerlayout()
    {
        barDrawerToggle = new ActionBarDrawerToggle(this, drawerlayout, toolbar, R.string.open, R.string.close)
        {
        };
        barDrawerToggle.syncState();
        drawerlayout.addDrawerListener(barDrawerToggle);
    }

    @Override
    public void onBackPressed()
    {
        //super.onBackPressed();
        if (drawerlayout.isDrawerOpen(container_left))
            drawerlayout.closeDrawer(container_left);
        else if (drawerlayout.isDrawerOpen(container_right))
            drawerlayout.closeDrawer(container_right);
        else twoClickExit();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        if (loadingView.isAnimating())
            loadingView.stop();
    }

    /*********************************************
     * 双击退出
     */
    private long exitTime = 0;
    private ProgressToast progressToast;
    private int progress;

    private void twoClickExit()
    {
        if ((System.currentTimeMillis() - exitTime) > 2000)
        {
            progressToast = SuperToast.progressToast(MainActivity.this)
                    .setText("再次点击退出")
                    .setBackgroundColor(ProgressToast.Background.GRAY)
                    .setAnimations(ProgressToast.Animations.POPUP)
                    .setMaxProgress(200).show();
            new SHandler()
            {
                @Override
                public void thread(Handler handler)
                {
                    for (progress = 1; progress <= 200; progress++)
                    {
                        handler.sendEmptyMessage(0);
                        Utils.sleep(10);
                    }
                }

                @Override
                public void handler()
                {
                    progressToast.setProgress(progress);
                }
            }.startByCustom();
            exitTime = System.currentTimeMillis();
        }
        else
            finish();
    }
}


